import "./header.js"
import {fillFilms} from "./tools.js"

const getFilms = function(current = 1,size = 3){
    $.ajax({
        type:"get",
        url:"/api/films",
        data:{
            current,size
        },
        success:function(data){
            fillFilms($(".films"),data.rows);
            let paginationStr = "";
            for(let i = 1;i <= data.totalPage;i++){
                if(i == data.current){
                    paginationStr += `<a class="page-number active" >${i}</a>`;
                }else{
                    paginationStr += `<a class="page-number">${i}</a>`;
                }
            }
            if(data.current > 1){
                paginationStr = `<a class="page-prev">上一页</a>`+paginationStr;
            }
            if(data.current < data.totalPage){
                paginationStr += `<a class="page-next">下一页</a>`;
            }
            
            $(".pagination").html(paginationStr);
            $(".page-number").click(function(){
                getFilms($(this).text());
            });
            $(".page-prev").click(function(){
                getFilms(data.current - 1);
            });
            $(".page-next").click(function(){
                getFilms(parseInt(data.current) + 1);
            });
    
        }
    });
}
getFilms();


